﻿using UnityEngine;
using System.Collections;
/// <summary>
/// 定义了统一的ui功能接口（事件、开关、动画、声音）
/// 同步、异步加载状态（定义一组常量）
/// </summary>
/// 
namespace FrameWork
{
	/// <summary>
	///抽象类 
	/// </summary>
    public abstract class BaseUI : MonoBehaviour
    {

        #region cache Transform & GameObject
        private GameObject _cacheGameObject = null;
        public GameObject CacheGameObject
        {
            get
            {
                if (null == _cacheGameObject)
                    _cacheGameObject = this.gameObject;
                return _cacheGameObject;
            }
        }

        private Transform _cacheTransform;
        public Transform CacheTransform
        {
            get
            {
                if (null == _cacheTransform)
                    _cacheTransform = this.transform;
                return _cacheTransform;
            }
        }
        #endregion

		#region  state & type
		
		protected EnumObjectState _state = EnumObjectState.None;

		public event StateChangeEvent StateChangeed;

		public EnumObjectState State
		{
			protected get
			{
				return this._state;
			}
			set
			{
				EnumObjectState oldState = this._state;
				this._state = value;
				if(null != StateChangeed)
					StateChangeed(this,this._state,oldState);
			}
		}

		public abstract EnumUIType GetUIType ();
		
		#endregion


        // Use this for initialization
        void Start()
        {
			OnStart ();
        }

		void Awake()
		{
            this.State = EnumObjectState.Init;
			OnAwake ();
		}

        // Update is called once per frame
        void Update()
        {
            //UI状态准备好了，再更新Update
			if (this._state == EnumObjectState.Ready)
				OnUpdate (Time.deltaTime);
        }
		/// <summary>
		/// Release this instance.资源卸载
		/// </summary>
		public void Release()
		{
			this.State = EnumObjectState.Closeing;
			GameObject.Destroy (this.gameObject);
			OnRelease ();
		}

		void OnDestroy()
		{
            this.State = EnumObjectState.Closeing;
            OnRelease();
		}

		/// <summary>
		///纯虚函数 
		/// </summary>
		protected virtual void OnStart()
		{
            

		}
		protected virtual void OnAwake()
		{
            this.State = EnumObjectState.loading;
			OnPlayOpenUIAudio ();
		}
        protected virtual void OnRelease()
		{
			this.State = EnumObjectState.None;
			OnPlayCloseUIAudio ();
		}
		protected virtual void OnUpdate(float deltaTime){}
		protected virtual void OnLoadData(){
		}
		protected virtual void OnPlayOpenUIAudio()
		{

		}

		protected virtual void OnPlayCloseUIAudio()
		{
			
		}

		protected virtual void SetUI(params object[] uiParms)
		{
			this.State = EnumObjectState.loading;
		}

        protected virtual void SetUIparam(params object[] uiParams)
        {

        }

		public void SetUIWhenOpening(params object[] uiParms)
		{
			SetUI (uiParms);
            CoroutineController.Instance.StartCoroutine(LoadDataAsyn());
		}

        /// <summary>
        /// 异步加载UI数据
        /// </summary>
        /// <returns></returns>
		private IEnumerator LoadDataAsyn()
		{
			yield return new WaitForSeconds(0);
            if (this.State == EnumObjectState.loading)
            {
				this.OnLoadData();
                this.State = EnumObjectState.Ready;
			}
		}


    }
}

